/**
 Copyright (c) 2007, Alek Dembowski

 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 */

package com.darkpegasus.trillian.parser;

import com.darkpegasus.trillian.exception.ParseException;
import com.darkpegasus.util.IOUtil;
import junit.framework.TestCase;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: alek
 * Date: Nov 5, 2007
 * Time: 3:13:47 PM
 * To change this template use File | Settings | File Templates.
 */
public class LogParserTC extends TestCase {

    public void testParseFile() throws Exception {
        LogParser parser = new LogParser();
        Map<Date, Collection<AbstractTrillianEvent>> eventHistory = parser.parseFile("test_data/astrid721.xml");

        assertNotNull(eventHistory);
        assertEquals("34 events in the file", 34, countEvents(eventHistory));

        eventHistory = parser.parseFile("test_data/bubbi.xml");

        assertNotNull(eventHistory);
        assertEquals("32 events in the file", 32, countEvents(eventHistory));

        eventHistory = parser.parseFile("test_data/Daem0nKn1ght.xml");

        assertNotNull(eventHistory);
        assertEquals("53 events in the file", 53, countEvents(eventHistory));
    }

    public void testLogFilePerf() throws Exception {
        String localBigFile = "F:\\projects\\encinarus@hotmail.com.xml";

        if(IOUtil.fileExists(localBigFile)){
            LogParser parser = new LogParser();

            Map<Date, Collection<AbstractTrillianEvent>> eventHistory = parser.parseFile(localBigFile);

            System.out.println(countEvents(eventHistory) + " events parsed from the 24mb log file");
        } else {
            System.out.println("Skipping test, file not found");
        }
    }

    private int countEvents(Map<Date, Collection<AbstractTrillianEvent>> eventHistory) {
        int eventCount = 0;
        for(Collection<AbstractTrillianEvent> dayEvents : eventHistory.values()){
            eventCount += dayEvents.size();
        }
        return eventCount;
    }

    public void testParseMessage() throws Exception {
        String messageXml = "<message type=\"outgoing_privateMessage\" time=\"1176950709\" medium=\"MSN\" to=\"harbinder_bhogan%40hotmail%2Ecom\" from=\"encinarus%40hotmail%2Ecom\" from_display=\"Alek\" text=\"brb\"/>";

        Message message = (Message) LogParser.parseEvent(messageXml);

        assertNotNull(message);
        assertEquals(LogEntry.parseTimestamp("1176950709"), message.getTimestamp());
        assertEquals("brb", message.getMessageText());
        assertEquals("encinarus@hotmail.com", message.getSenderId());
        assertEquals("Alek", message.getSenderDisplayName());
        assertEquals("", message.getLocation());
        assertEquals(Message.Direction.Outgoing, message.getDirection());

        //------------------
        messageXml = "<message type=\"outgoing_groupMessage\" time=\"1194374564\" medium=\"AIM\" from=\"Daem0nKn1ght\" location=\"bubbi\" text=\"hi%21\"/>";

        message = (Message) LogParser.parseEvent(messageXml);

        assertNotNull(message);
        assertEquals(LogEntry.parseTimestamp("1194374564"), message.getTimestamp());
        assertEquals("hi!", message.getMessageText());
        assertEquals("Daem0nKn1ght", message.getSenderId());
        assertEquals("Daem0nKn1ght", message.getSenderDisplayName());
        assertEquals("bubbi", message.getLocation());
        assertEquals(Message.Direction.Outgoing, message.getDirection());

    }

    public void testParseMessageError() throws Exception {
        String corruptXml = "message type=\"outgoing_privateMessage\" time=\"1176950709\" medium=\"MSN\" to=\"harbinder_bhogan%40hotmail%2Ecom\" from=\"encinarus%40hotmail%2Ecom\" from_display=\"Alek\" text=\"brb\"/>";

        try {
            Message testMessage = (Message) LogParser.parseEvent(corruptXml);
        } catch (ParseException e) {
            //Good, what we wanted
            return;
        }

        fail("Garbled XML should've thrown a ParseException");
    }

    public void testParseStatusUpdate() throws Exception {
        String statusUpdateXml = "<status type=\"Offline\" time=\"1177877866\" medium=\"MSN\" from=\"harbinder_bhogan%40hotmail%2Ecom\"/>";

        StatusUpdate statusUpdate = (StatusUpdate) LogParser.parseEvent(statusUpdateXml);

        assertNotNull(statusUpdate);

        assertEquals(StatusUpdate.Status.Offline, statusUpdate.getStatus());
        assertEquals(LogEntry.parseTimestamp("1177877866"), statusUpdate.getTimestamp());
        assertEquals("harbinder_bhogan@hotmail.com", statusUpdate.getSenderId());

        //----------------------
        statusUpdateXml = "<status type=\"offline\" time=\"1177877866\" medium=\"MSN\" from=\"harbinder_bhogan%40hotmail%2Ecom\"/>";
        statusUpdate = (StatusUpdate) LogParser.parseEvent(statusUpdateXml);

        assertNotNull(statusUpdate);

        assertEquals(StatusUpdate.Status.Offline, statusUpdate.getStatus());
        assertEquals(LogEntry.parseTimestamp("1177877866"), statusUpdate.getTimestamp());
        assertEquals("harbinder_bhogan@hotmail.com", statusUpdate.getSenderId());

        //----------------------
        statusUpdateXml = "<status type=\"online\" time=\"1177877866\" medium=\"MSN\" from=\"harbinder_bhogan%40hotmail%2Ecom\"/>";
        statusUpdate = (StatusUpdate) LogParser.parseEvent(statusUpdateXml);

        assertNotNull(statusUpdate);

        assertEquals(StatusUpdate.Status.Online, statusUpdate.getStatus());
        assertEquals(LogEntry.parseTimestamp("1177877866"), statusUpdate.getTimestamp());
        assertEquals("harbinder_bhogan@hotmail.com", statusUpdate.getSenderId());

        //----------------------
        statusUpdateXml = "<status type=\"idle\" time=\"1177877866\" medium=\"MSN\" from=\"harbinder_bhogan%40hotmail%2Ecom\"/>";
        statusUpdate = (StatusUpdate) LogParser.parseEvent(statusUpdateXml);

        assertNotNull(statusUpdate);

        assertEquals(StatusUpdate.Status.Idle, statusUpdate.getStatus());
        assertEquals(LogEntry.parseTimestamp("1177877866"), statusUpdate.getTimestamp());
        assertEquals("harbinder_bhogan@hotmail.com", statusUpdate.getSenderId());

    }

    public void testParseStatusUpdateError() throws Exception {
        String corruptXml = "haha this isn't even XML";

        try {
            StatusUpdate update = (StatusUpdate) LogParser.parseEvent(corruptXml);
        } catch (ParseException e) {
            //Good, what we wanted
            return;
        }

        fail("Corrupt XML should've thrown a ParseException");
    }

    public void testInvalidStatusTypeError() throws Exception {
        String corruptXml = "<status type=\"american_idle\" time=\"1177877866\" medium=\"MSN\" from=\"harbinder_bhogan%40hotmail%2Ecom\"/>";

        try {
            StatusUpdate update = (StatusUpdate) LogParser.parseEvent(corruptXml);
        } catch (ParseException e) {
            //Good, what we wanted
            return;
        }

        fail("Corrupt XML should've thrown a ParseException");
    }

    public void testParseSessionUpdate() throws Exception{
        String sessionXml = "<session type=\"start\" time=\"1194374107\" medium=\"AIM\" to=\"daem0nkn1ghtchat560\" from=\"Daem0nKn1ght\" location=\"daem0nkn1ghtchat560\"/>";

        SessionUpdate update = (SessionUpdate) LogParser.parseEvent(sessionXml);

        assertNotNull(update);
        assertEquals(LogEntry.parseTimestamp("1194374107"), update.getTimestamp());
        assertEquals("daem0nkn1ghtchat560", update.getLocation());
        assertEquals("Daem0nKn1ght", update.getSenderId());
        assertEquals(SessionUpdate.UpdateType.Start, update.getUpdateType());

        sessionXml = "<session type=\"STOP\" time=\"1194374107\" medium=\"AIM\" to=\"daem0nkn1ghtchat560\" from=\"Daem0nKn1ght\" location=\"daem0nkn1ghtchat560\"/>";

        update = (SessionUpdate) LogParser.parseEvent(sessionXml);

        assertNotNull(update);
        assertEquals(LogEntry.parseTimestamp("1194374107"), update.getTimestamp());
        assertEquals("daem0nkn1ghtchat560", update.getLocation());
        assertEquals("Daem0nKn1ght", update.getSenderId());
        assertEquals(SessionUpdate.UpdateType.Stop, update.getUpdateType());
    }

}
